package com.xgg.taiwuinfo.filter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.xgg.taiwuinfo.adapter.SpinAdapter;
import com.xgg.taiwuinfo.filter.fc.FCCastCompareNum;
import com.xgg.taiwuinfo.filter.fc.FCCuipoBodyEqualSelect;
import com.xgg.taiwuinfo.filter.fc.FCCuipoDamageCompareNum;
import com.xgg.taiwuinfo.filter.fc.FCCuipoEffectCompareSelect;
import com.xgg.taiwuinfo.filter.fc.FCCuipoHitCompareNum;
import com.xgg.taiwuinfo.filter.fc.FCCuipoHitCompareSelect;
import com.xgg.taiwuinfo.filter.fc.FCElementEqualSelect;
import com.xgg.taiwuinfo.filter.fc.FCGridOccuppyCompareSelect;
import com.xgg.taiwuinfo.filter.fc.FCInheritEqualSelect;
import com.xgg.taiwuinfo.filter.fc.FCLevelCompareSelect;
import com.xgg.taiwuinfo.filter.fc.FCNameContainEdit;
import com.xgg.taiwuinfo.filter.fc.FCNeigongGridCompareSelect;
import com.xgg.taiwuinfo.filter.fc.FCTypeEqualSelect;
import com.xgg.taiwuinfo.filter.fc.FCYunGongActionCompareNum;
import com.xgg.taiwuinfo.filter.fc.FCYunGongAttackCompareNum;
import com.xgg.taiwuinfo.gongfa.base.Gongfa;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

abstract public class FilterComponent {
    protected int mTypeId;
    public int getType() { return mTypeId; }

    public FilterComponent()
    {
        delete_listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            if (mHandler != null){
                Message msg = mHandler.obtainMessage();
                msg.what = MSG_DELETE;
                msg.obj = FilterComponent.this;
                msg.sendToTarget();
            }
            }
        };
    }

    static final public int MSG_DELETE = 1001;

    abstract public void discard();
    abstract public String FilterGongfa(Gongfa gongfa);
    abstract public int getItemLayout();
    abstract public List<Map<String, String>> getFieldString(Context ctx);
    abstract public List<Map<String, String>> getCompareString(Context ctx);
    abstract public List<Map<String, String>> getValueMap(Context ctx);
    protected String FieldStr = "";
    protected String CompStr = "";
    protected String ValueStr = "";
    abstract public void save();
    abstract public boolean isEdited();

    public AdapterView.OnItemSelectedListener field_listener;
    public AdapterView.OnItemSelectedListener compare_listener;
    public AdapterView.OnItemSelectedListener value_listener;
    public View.OnClickListener delete_listener;
    public TextWatcher text_listener;

    public void selectField(Spinner spinner){}
    public void selectCompare(Spinner spinner){}
    public void selectValue(Spinner spinner){}

    static private Handler mHandler = null;
    static public void setHandler(Handler handler) { mHandler = handler; }

    static public FilterComponent LoadJson(JSONObject obj , int tid , Context ctx)
    {
        try {
            int id ;
            if (obj != null) {
                id = obj.getInt("id");
            }
            else {
                id = tid;
            }
            switch (id)
            {
                case FT_TYPE_EQUAL_SELECT:
                    return new FCTypeEqualSelect(obj , ctx);
                case FT_ElEMENT_EQUAL_SELECT:
                    return new FCElementEqualSelect(obj , ctx);
                case FT_LEVEL_COMP_SELECT:
                    return new FCLevelCompareSelect(obj , ctx);
                case FT_INHERIT_EQUAL_SELECT:
                    return new FCInheritEqualSelect(obj , ctx);
                case FT_GRIDOCCUPY_COMP_SELECT:
                    return new FCGridOccuppyCompareSelect(obj);
                case FT_NEIGONGGRID:
                    return new FCNeigongGridCompareSelect(obj);
                case FT_NAME_CONTAIN_EDIT:
                    return new FCNameContainEdit(obj , ctx);
                case FT_CAST_COMAPRE_NUM:
                    return new FCCastCompareNum(obj);
                case FT_CUIPOBODY_EQUAL_SELECT:
                    return new FCCuipoBodyEqualSelect(obj);
                case FT_DAMAGE_COMPARE_NUM:
                    return new FCCuipoDamageCompareNum(obj);
                case FT_CUIPOEFFECT_COMPARE_SELECT:
                    return new FCCuipoEffectCompareSelect(obj);
                case FT_HIT_COMPARE_NUM:
                    return new FCCuipoHitCompareNum(obj);
                case FT_CUIPOHIT_COMPARE_SELECT:
                    return new FCCuipoHitCompareSelect(obj);
                case FT_YUNGONG_ACTOIN_COMPARE_NUM:
                    return new FCYunGongActionCompareNum(obj);
                case FT_YUNGONG_ATTACK_COMPARE_NUM:
                    return new FCYunGongAttackCompareNum(obj);
                default:
                    return null;
            }
        }
        catch (Exception e) {
            Log.d("LoadJson" , e.getLocalizedMessage());
        }
        return null;
    }

    abstract public JSONObject getJsonObj();

    public ArrayAdapter<String> getFieldAdapter(Context ctx){
        SpinAdapter spinnerAdapter = new SpinAdapter(ctx , android.R.layout.simple_spinner_item , getFieldString(ctx));
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        return spinnerAdapter;
    }

    public ArrayAdapter<String> getCompareAdapter(Context ctx){
        SpinAdapter spinnerAdapter = new SpinAdapter(ctx , android.R.layout.simple_spinner_item , getCompareString(ctx));
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        return spinnerAdapter;
    }

    public SpinAdapter getValueAdapter(Context ctx){
        SpinAdapter spinnerAdapter = new SpinAdapter(ctx , android.R.layout.simple_spinner_item , getValueMap(ctx));
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        return spinnerAdapter;
    }

    protected void AddMap(List<Map<String, String>> list , String field , String value)
    {
        Map<String, String> map = new HashMap<>();
        map.put("f", field);
        map.put("v", value);
        list.add(map);
    }

    abstract public String getDesc();// { return String.format("%d 号过滤条件没有输出描述字符串\r\n" , mTypeId) ; }

    static final public int FC_EQUAL = 2;
    static final public int FC_GREATERCONTAIN = 1;
    static final public int FC_GREATER = 0;
    static final public int FC_SMALLER = 4;
    static final public int FC_SMALLERCONTAIN = 3;
    static final public int FC_NOTEQUAL = 5;

    static final public int FT_TYPE_EQUAL_SELECT = 10001;
    static final public int FT_ElEMENT_EQUAL_SELECT = 10002;
    static final public int FT_INHERIT_EQUAL_SELECT = 10003;
    static final public int FT_CUIPOBODY_EQUAL_SELECT = 10004;

    static final public int FT_LEVEL_COMP_SELECT = 20001;
    static final public int FT_GRIDOCCUPY_COMP_SELECT = 20002;

    static final public int FT_NEIGONGGRID = 30001;
    static final public int FT_CUIPOEFFECT_COMPARE_SELECT = 30002;
    static final public int FT_CUIPOHIT_COMPARE_SELECT = 30003;

    static final public int FT_NAME_CONTAIN_EDIT = 40001;

    static final public int FT_CAST_COMAPRE_NUM = 50001;
    static final public int FT_DAMAGE_COMPARE_NUM = 50002;
    static final public int FT_HIT_COMPARE_NUM = 50003;
    static final public int FT_YUNGONG_ACTOIN_COMPARE_NUM = 50004;
    static final public int FT_YUNGONG_ATTACK_COMPARE_NUM = 50005;
}


